package com.hnevc.zz.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hnevc.zz.model.DataInfo;

import java.util.ArrayList;
import java.util.List;

public class RechargeDao {
    public Context context;
    RechargeDatabase rechargeDatabase;

    public RechargeDao(Context context, RechargeDatabase rechargeDatabase) {
        this.context = context;
        this.rechargeDatabase = rechargeDatabase;
    }
    /**
     * 添加数据
     */
    public boolean insertMoney(DataInfo  dataInfo){
        SQLiteDatabase database = rechargeDatabase.getWritableDatabase();
        long i=0;
        if (database.isOpen()) {
            ContentValues values = new ContentValues();
            values.put("id", dataInfo.getId());
            values.put("phone", dataInfo.getPhone());
            values.put("flow", dataInfo.getFlow());
            values.put("date", dataInfo.getDate());
            i=database.insert("recordflow", "_id", values);
            database.close();
        }
        return i>0?true:false;
    }
    /**
     * 查询数据列表  话费充值记录集合
     */
    public List<DataInfo> querDataInfo(){
        SQLiteDatabase db = rechargeDatabase.getReadableDatabase();
        List<DataInfo> dataInfos=null;
        DataInfo dataInfo=null;
        Cursor cursor = null;
        if (db.isOpen()) {
            cursor=db.rawQuery("select * from recordflow", null);
            if (cursor!=null) {
                dataInfos = new ArrayList<>();
                while (cursor.moveToNext()) {
                    String id = cursor.getString(cursor.getColumnIndex("id"));
                    String phone = cursor.getString(cursor.getColumnIndex("phone"));
                    String flow = cursor.getString(cursor.getColumnIndex("flow"));
                    String date = cursor.getString(cursor.getColumnIndex("date"));
                    dataInfo = new DataInfo(id, phone, flow, date);
                    dataInfos.add(dataInfo);
                }
                cursor.close();
            }
            db.close();

        }
        return dataInfos;
    }
}
